package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.JurisdictionDao;
import com.zy.edu.service.ClassinfoService;
import com.zy.edu.service.JurisdictionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.service.SchoolService;
import com.zy.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-13
 */
@Service
@Transactional
public class JurisdictionServiceImpl extends ServiceImpl<JurisdictionDao, Jurisdiction> implements JurisdictionService {

    @Resource
    UserService userService;

    @Resource
    SchoolService schoolService;

    @Resource
    ClassinfoService classinfoService;
    @Override
    public Map saveOrUpdate(Jurisdiction jurisdiction) {
        List<Jurisdiction> tempList = this.baseMapper.selectList(new EntityWrapper<Jurisdiction>()
                .eq("name", jurisdiction.getName()));
        if(StringUtils.isEmpty(jurisdiction.getId())){
            jurisdiction.setId("j-"+UUID.randomUUID().toString().replaceAll("-",""));
            jurisdiction.setCreateTime(new Date());
            jurisdiction.setStatus(1);
            if(tempList.size()>0){
                return ResponseInfo.error("辖区名称已存在，不能创建");
            }else {
                this.baseMapper.insert(jurisdiction);
            }
        }else{
            // 若存在名称相同的辖区，且不是自己，则提示重复
            if(tempList.size()>0 && !tempList.get(0).getId().equals(jurisdiction.getId())){
                return ResponseInfo.error("辖区名称已存在，不能修改");
            }else {
                this.baseMapper.updateById(jurisdiction);
            }
        }
        //更新静态数据
        SystemParam.JURISDICTIONS.put(jurisdiction.getId(),jurisdiction.getName());
        return ResponseInfo.success(null);
    }

    @Override
    public Map list(Jurisdiction jurisdiction, InputParam inputParam) {
        EntityWrapper<Jurisdiction> entityWrapper = new EntityWrapper<Jurisdiction>();
        if(!StringUtils.isEmpty(jurisdiction.getName())){
            entityWrapper.like("name",jurisdiction.getName());
        }
        if(!StringUtils.isEmpty(jurisdiction.getId())){
            entityWrapper.eq("id",jurisdiction.getId());
        }
        if(!StringUtils.isEmpty(jurisdiction.getProvince())){
            entityWrapper.eq("province",jurisdiction.getProvince());
        }
        if(!StringUtils.isEmpty(jurisdiction.getCity())){
            entityWrapper.eq("city",jurisdiction.getCity());
        }
        entityWrapper.eq("status",1);
        entityWrapper.orderBy("create_time",false);

        //查分页
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0){
            Page<Jurisdiction> page = new Page<Jurisdiction>(inputParam.getPage(), inputParam.getSize());
            List<Jurisdiction> tempList = this.baseMapper.selectPage(page,entityWrapper);
            Integer total = this.baseMapper.selectCount(entityWrapper);

            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", total);
            return ResponseInfo.success(resultMap);
        }else {//查全部
            List<Jurisdiction> tempList = this.baseMapper.selectList(entityWrapper);
            return ResponseInfo.success(tempList);
        }

    }

    @Override
    public Map dele(Jurisdiction jurisdiction) {
        if(!StringUtils.isEmpty(jurisdiction.getId())){
            //设置为不可用
            jurisdiction.setStatus(0);
            this.baseMapper.updateById(jurisdiction);
            SystemParam.JURISDICTIONS.remove(jurisdiction.getId());
            //同时修改下属的学校和班级、用户
            InputParam inputParam = new InputParam();
            inputParam.setJurisId(jurisdiction.getId());
            schoolService.deleByJruisId(inputParam);
            classinfoService.deleByJruisId(inputParam);
            userService.deleByJruisId(inputParam);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public List<Jurisdiction> findBySuper() {
        return this.baseMapper.findBySuper();
    }

    @Override
    public Map getById(Jurisdiction jurisdiction, InputParam inputParam) {
        if(StringUtils.isEmpty(jurisdiction.getId())) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        return ResponseInfo.success(this.selectById(jurisdiction.getId()));
    }

    @Override
    public Map getJurisByRole(Integer roleType,String userId) {

        if(roleType !=null
            &&!StringUtils.isEmpty(userId)){
            User user = userService.selectById(userId);
            Jurisdiction jurisdiction = new Jurisdiction();
            //若不是超管，只能查自己的辖区
            if(!user.getRoleType().equals(Role.SUPER)){
                jurisdiction.setId(user.getJurisId());
            }
            return this.list(jurisdiction, null);
        }else{
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
    }
}
